#!/usr/bin/env python3
import requests
import json

def add_test_comments():
    """添加测试评论到内容中"""
    base_url = "http://localhost:8080/api"
    
    # 登录获取token
    login_data = {"usernameOrEmail": "testuser", "password": "password"}
    response = requests.post(f"{base_url}/auth/login", json=login_data)
    
    if response.status_code != 200:
        print(f"❌ 登录失败: {response.text}")
        return
    
    result = response.json()
    if not result.get('success'):
        print(f"❌ 登录失败: {result.get('message')}")
        return
    
    token = result['data']['token']
    headers = {
        'Content-Type': 'application/json',
        'Authorization': f'Bearer {token}'
    }
    
    print(f"✅ 登录成功，用户: {result['data']['username']}")
    
    # 要添加评论的内容列表
    contents = [17, 18, 16, 15]
    
    # 测试评论
    test_comments = [
        "这个内容真的很不错！👍",
        "看起来很有趣的分享",
        "感谢分享，学到了很多",
        "这个创意很棒！",
        "期待更多这样的内容",
        "非常有用的信息",
        "质量很高的作品！",
        "继续加油！✨"
    ]
    
    comment_count = 0
    
    for content_id in contents:
        print(f"\n--- 为内容 {content_id} 添加评论 ---")
        
        # 为每个内容添加2-3个评论
        num_comments = min(3, len(test_comments) - comment_count)
        
        for i in range(num_comments):
            comment_text = test_comments[comment_count]
            comment_data = {"text": comment_text}
            
            try:
                response = requests.post(
                    f"{base_url}/contents/{content_id}/comments",
                    json=comment_data,
                    headers=headers
                )
                
                if response.status_code == 200:
                    result = response.json()
                    if result.get('success'):
                        print(f"✅ 添加评论成功: {comment_text}")
                    else:
                        print(f"❌ 添加评论失败: {result.get('message')}")
                else:
                    print(f"❌ 添加评论失败: HTTP {response.status_code}")
                    
            except Exception as e:
                print(f"❌ 添加评论异常: {str(e)}")
            
            comment_count += 1
            
            if comment_count >= len(test_comments):
                break
        
        if comment_count >= len(test_comments):
            break
    
    print(f"\n=== 完成！总共添加了 {comment_count} 个测试评论 ===")
    
    # 验证评论已添加
    print("\n--- 验证评论 ---")
    for content_id in contents:
        try:
            response = requests.get(f"{base_url}/contents/{content_id}/comments?page=0&size=5")
            if response.status_code == 200:
                result = response.json()
                if result.get('success'):
                    comments = result['data']['content']
                    print(f"内容 {content_id}: {len(comments)} 个评论")
                    for comment in comments[:2]:  # 显示前2个评论
                        print(f"  - {comment['userName']}: {comment['text']}")
                else:
                    print(f"内容 {content_id}: 获取失败")
            else:
                print(f"内容 {content_id}: HTTP {response.status_code}")
        except Exception as e:
            print(f"内容 {content_id}: 异常 {str(e)}")

if __name__ == "__main__":
    add_test_comments() 